package gemini.castor.ui.client.page.content.store.pricelist.pricelistform;

import gemini.basic.model.ProductType;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartEvent;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartItem;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class PriceListFormPresenter extends
		CastorWidgetPresenter<PriceListFormPresenter.Display> {

	@Inject
	public PriceListFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getAddToCartClickHandlers();

		String getSelectedListBoxValue();

		HasChangeHandlers getListBoxChangeHandler();

		void setObject(PriceListFormObject object);

		void loadData();

		void setProductTypes(List<ProductType> productTypes);

		PriceListFormObject getObject();

		void setProductOneTypes(ProductType productTypes);

		void onChangedItems();

	}

	private PriceListFormObject inputObject;
	private List<ProductType> productTypes;
	private ProductType productOneType;
	

	public static final Place PLACE = new Place(PlaceConstants.PLACE_PRICE_LIST);

	@Override
	protected void onBind() {
		registerHandler(display.getAddToCartClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ShoppingCartEvent cartEvent = new ShoppingCartEvent(ShoppingCartEvent.ForwardType.ADD);
				cartEvent.setItems(createShoppingCartItems());
				eventBus.fireEvent(cartEvent);
			}
		}));
		registerHandler(display.getListBoxChangeHandler().addChangeHandler(
                new ChangeHandler() {
                        
                        @Override
                        public void onChange(ChangeEvent arg0) {
                        	eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
                        	getProducts(display.getSelectedListBoxValue());
                        }
                }));

		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getProductTypeService().getAllCategory(
				new AbstractAsyncCallback<ArrayList<ProductType>>() {

					@Override
					public void onSuccess(ArrayList<ProductType> result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						if (result != null) {
							productTypes = result;
							display.setProductTypes(productTypes);
							getProducts(result.get(0).getCode());
							display.loadData();
						} else {
							GuiUtils.alert("Cannot find any product type in db");
						}
					}
				});
	}

	private List<ShoppingCartItem> createShoppingCartItems() {
		List<ShoppingCartItem> items = new ArrayList<ShoppingCartItem>();
		for (ShoppingCartItem it : inputObject.getItems()) {
			if (it.isSelected()) {
				items.add(it);
			}
		}

		return items;
	}
	
	private void getProducts(String code){
		ServiceCatalog.getProductTypeByCodeService().getCategoryByCode(code, 
				new AbstractAsyncCallback<ProductType>() {

			@Override
			public void onSuccess(ProductType result) {
				eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
				if (result != null) {
					productOneType = result;
					inputObject = new PriceListFormObject();
					display.setObject(inputObject);
					display.setProductOneTypes(productOneType);
					display.onChangedItems();
				} else {
					GuiUtils.alert("Cannot find any product type in db");
				}
			}
		});
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void setInputObject(Object ob) {
		inputObject = (PriceListFormObject) ob;
	}
}
